consequences of pass by reference
implementation
Any combination of arguments can be passed by reference by prefixing them with @.
pass by reference example
The following contrived program segment demonstrates that each and every time a function
is invoked, arguments can be passed to it in any mix of pass by value and pass by
reference:
FUNCTION IncSum (x, y, z) ' IncSum takes three arguments...
INC x : INC y : INC z ' increments them...
END FUNCTION (x + y + z) ' end of function
'
' . . .
'
FUNCTION TestByRef ()
a = 0 : b = 10 : c = 20 ' give initial values to a, b, c
'
'
retval
after after after
x = IncSum ( a, b, c) ' x = 33: a = 0: b = 10: c = 20
x = IncSum ( a, b, @c) ' x = 33: a = 0: b = 10: c = 21
x = IncSum ( a, @b, c) ' x = 34: a = 0: b = 11: c = 21
x = IncSum ( a, @b, @c) ' x = 35: a = 0: b = 12: c = 22
x = IncSum (@a, b,c) ' x = 37: a = 1: b = 12: c = 22
x = IncSum (@a, b, @c) ' x = 38: a = 2: b = 12: c = 23
x = IncSum (@a, @b, c) ' x = 40: a = 3: b = 13: c = 23
x = IncSum (@a, @b, @c) ' x = 42: a = 4: b = 14: c = 24
END FUNCTION
Function IncSum() is called eight different ways. Conventional pass by value
languages would require eight separate functions.
Because various C implementations handle arguments differently, arguments cannot be passed
by reference to C functions.
pass by address
Pass by Address is provided for calling C functions that expect argument addresses.
To pass by address, an & (address operator) can be prefixed to numeric,
composite, string, array, and function arguments to produce an XLONG address. Pass
by address is of little value within native programs because pass by value and pass by
reference are sufficient, more efficient, and avoid allocation problems caused by pass by
address.
argument checking
Functions require a specific kind (variable or array), and data type for each argument, as
declared in the DECLARE FUNCTION parameter list and specified in the FUNCTION argument
list.
Passing an array where a variable is expected, or vice versa, causes a kind mismatch
error. The compiler compares the data type of each argument passed to a function
against the declared type. If they are the same, the argument is passed directly.
If they are different types, but both numeric, the argument is converted to the
declared type before it is passed. Otherwise a type-mismatch error occurs.
Arrays can only be passed by reference because accidentally passing an array by value
would degrade overall program performance by a factor of hundreds to millions.